package com.youlai.boot.modules.area.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.area.converter.AreaProductConverter;
import com.youlai.boot.modules.area.mapper.AreaProductMapper;
import com.youlai.boot.modules.area.model.AreaProduct;
import com.youlai.boot.modules.area.model.AreaProductForm;
import com.youlai.boot.modules.area.model.AreaProductVO;
import com.youlai.boot.modules.area.service.AreaProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AreaProductServiceImpl extends ServiceImpl<AreaProductMapper, AreaProduct> implements AreaProductService {

    private final AreaProductConverter areaProductConverter;

    @Override
    public Page<AreaProductVO> getAreaProductPage(Long areaId, Integer showFlag, int pageNum, int pageSize) {
        Page<AreaProductVO> page = new Page<>(pageNum, pageSize);
        return baseMapper.getAreaProductPage(page, areaId, showFlag);
    }

    @Override
    public List<AreaProductVO> listByAreaId(Long areaId) {
        Page<AreaProductVO> page = this.getAreaProductPage(areaId, null, 1, 999);
        return page.getRecords();
    }

    @Override
    public AreaProductVO getAreaProductById(Long id) {
        AreaProduct areaProduct = this.getById(id);
        return areaProductConverter.toVO(areaProduct);
    }

    @Override
    public Long saveAreaProduct(AreaProductForm form) {
        AreaProduct areaProduct = areaProductConverter.toEntity(form);
        this.save(areaProduct);
        return areaProduct.getId();
    }

    @Override
    public void updateAreaProduct(AreaProductForm form) {
        AreaProduct areaProduct = areaProductConverter.toEntity(form);
        this.updateById(areaProduct);
    }

    @Override
    public boolean deleteAreaProduct(Long id) {
        return this.removeById(id);
    }
}